package b1.controls.tab
{
	import flash.display.Sprite;

	public class Tab extends Sprite
	{
		public var tabPages:Array;
		private var _currentPageIndex:int;
		private var pageSelector:TabPageSelector;
		
		public var tabName:String;
		public var items:Array;
		public var type:int;
		private var wNum:int;
		private var hNum:int; 
		private var gap:int;
		private var refreshFunc:Function;
		
		//type: 0,商店的对话框； 1，回家界面中的包裹； 2，作案界面中的包裹；3,探案、作案、跑路的对话框
		public function Tab(name:String, wNum:int = 5, hNum:int = 3, gap:int = 5, type:int = 0)
		{
			super();
			this.tabName = name;
			this.wNum = wNum;
			this.hNum = hNum;
			this.gap = gap;
			this.type = type;
			this.tabPages = new Array();
			this.items = new Array();
			_currentPageIndex = 0;
			pageSelector = new TabPageSelector(this);
			if (type == 0)
			{
				pageSelector.x = 250;
				pageSelector.y = 247;
			}
			else if (type == 1)
			{
				pageSelector.x = 72;
				pageSelector.y = 60;
			}
			else if (type == 2)
			{
				pageSelector.x = 70;
				pageSelector.y = 30;
			}
			else if (type == 3)
			{
				pageSelector.x = 257;
				pageSelector.y = 247;
			}
			addChild(pageSelector);
		}
		
		public function setRefreshFunc(refreshFunc:Function):void
		{
			this.refreshFunc = refreshFunc;
		}
		
		public function enable():void
		{
			if (refreshFunc != null)
				this.refreshFunc();
			this.visible = true;
		}
		
		public function disable():void
		{
			this.visible = false;
		}
		
		public function addItem(item:Sprite, width:int, height:int):void
		{
			items.push(item);
			if (items.length > wNum * hNum * this.tabPages.length)
			{
				var tabPage:TabPage = new TabPage(wNum, hNum, gap)
				if (currentPageIndex != this.tabPages.length)
				{
					tabPage.visible = false;
				}					
				tabPages.push(tabPage);
				//回家界面的下面导航时	
				if (this.type == 1)
				{
					tabPage.y = 30;
					tabPage.x = 140;	
				}
				//商店的对话框
				else if (this.type == 0)
				{
					tabPage.x = 90;
					tabPage.y = -13;
				}
				//作案，探案，跑路对话框
				else if (this.type == 3)
				{
					tabPage.x = 130;
					tabPage.y = -13;
				}
				else if (this.type == 2)
				{
					tabPage.y = 35;
					tabPage.x = 122;	
				}
				addChild(tabPage);
				if (this.type == 0 || this.type == 3)
				{
					pageSelector.refreshTotalPageNum();
					if (this.type == 0)
					{
						pageSelector.x = (750 - pageSelector.width) / 2;
					}
					else
					{
						pageSelector.x = (750 - pageSelector.width) / 2 + 15;
					}
				}
			}
			tabPages[tabPages.length - 1].addItem(item, width, height);
		}
		
		public function removeAll():void
		{
			while (tabPages.length > 0)
			{
				var tabPage:TabPage = tabPages.pop();
				removeChild(tabPage);
			}
			_currentPageIndex = 0;
			items = new Array();
		}
		
		public function get totalPageNum():int
		{
			return tabPages.length;
		}
		
		public function set currentPageIndex(pageIndex:int):void
		{
			if (pageIndex != _currentPageIndex)
			{
				tabPages[_currentPageIndex].visible = false;
				tabPages[pageIndex].visible = true;
				_currentPageIndex = pageIndex;
			} 
		}
		
		public function get currentPageIndex():int
		{
			return _currentPageIndex;
		}
		
	}
}